<template>
  <div v-if="pageStatus === 1">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form ref="queryFormRef" :inline="true" :model="paramsData" class="-mb-10px" label-width="auto">
        <el-form-item label="会议编号" prop="code">
          <el-input v-model="paramsData.code" class="!w-280px" clearable placeholder="请输入会议编号"
            @keyup.enter="handleQuery" />
        </el-form-item>
  
        <el-form-item label="会议名称" prop="name">
          <el-input v-model="paramsData.name" class="!w-280px" clearable placeholder="请输入会议名称" @keyup.enter="handleQuery" />
        </el-form-item>
  
        <el-form-item label="会议负责人" prop="leaderId">
          <div class="flex-s formInp">
            <div class="coord flex-s !w-280px" :class="paramsData.leaderName ? 'cursor_pointer' : ''">
              <div class="flex-b formInp" @click="addCompanies" @mouseenter="hoverIcon = true"
                  @mouseleave="hoverIcon = false">
                <span v-if="paramsData.leaderName">{{ paramsData.leaderName }}</span>
                <div v-else style="color: rgb(159 155 155);">请选择会议负责人</div>
                <el-icon
                  style="color: rgb(159 155 155);"
                  v-if="paramsData.leaderName && hoverIcon"
                  @click.stop="paramsData.leaderName = ''; paramsData.leaderId = '';"
                ><CircleClose /></el-icon>
                <el-icon v-if="!paramsData.leaderName"><ArrowDown /></el-icon>
              </div>
            </div>
          </div>
        </el-form-item>
  
        <el-form-item label="会议部门" prop="department">
          <el-tree-select
            v-model="paramsData.department"
            class="!w-280px"
            :data="deptTree"
            filterable
            clearable
            :props="defaultProps"
            check-strictly
            default-expand-all
            placeholder="请选择会议部门"
            value-key="deptId"
          />
        </el-form-item>
      
        <el-form-item label="会议类型" prop="type">
          <el-select v-model="paramsData.type" class="!w-280px" clearable placeholder="请选择会议类型">
            <el-option v-for="item in meetingTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
  
        <el-form-item label="会议时间" prop="meetingTime">
          <el-date-picker
            v-model="paramsData.meetingTime"
            class="!w-280px"
            type="date"
            placeholder="请选择会议时间"
            @change="e => paramsData.meetingTime = formatDateToYMD(e)"
          />
        </el-form-item>
  
        <el-form-item>
          <el-button v-hasPermi="['wise:project:query']" @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
          <el-button v-hasPermi="['wise:project:create']" plain type="primary" @click="openForm(null)">
            <Icon class="mr-5px" icon="ep:plus" />
            新增
          </el-button>
          <el-button v-hasPermi="['wise:project:export']" :loading="exportLoading" plain type="success"
            @click="handleExport">
            <Icon class="mr-5px" icon="ep:download" />
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

      <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="tableData" @selection-change="tableSelectionChange">
        <el-table-column type="selection" width="30" />
        <template v-for="item in tableColumn" :key="item.prop">
          <el-table-column
            :width="item.width"
            :align="item.align ? item.align : 'center'" 
            :label="item.label"
            :prop="item.prop"
          >
            <template #default="scope">
              <div v-if="item.prop === 'type'">
                <span>{{ meetingTypeList.find(item => item.value == scope.row.type)?.label || '-' }}</span>
              </div>
              <div v-else-if="item.prop == 'createTime'">
                <p>创建时间：{{ scope.row.createTime ? formatDate(scope.row.createTime) : '-' }}</p>
                <p>更新时间：{{ scope.row.updateTime ? formatDate(scope.row.updateTime) : '-' }}</p>
              </div>
              <div v-else-if="item.prop == 'creator'">
                <p>创建：{{ scope.row.creator ? scope.row.creator : '-' }}</p>
                <p>更新：{{ scope.row.updater ? scope.row.updater : '-' }}</p>
              </div>
              <div v-else>
                {{ scope.row[item.prop] ? scope.row[item.prop] : '-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <el-table-column :width="200" align="center" label="操作">
          <template #default="scope">
            <el-button v-hasPermi="['wise:project:update']" link type="primary" @click="openForm(scope.row.id)">
              编辑
            </el-button>
            <el-button v-hasPermi="['wise:project:delete']" link type="danger" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>


      <!-- 分页 -->
      <Pagination v-model:limit="paramsData.pageSize" v-model:page="paramsData.pageNo" :total="total" @pagination="getList" />
    </ContentWrap>
  </div>



  <!-- 新增、编辑页 -->
  <MeetingForm v-else ref="formRef" />



  <!-- 勾选数据 -->
  <personalDialog ref="personalDialogRef" @change="personalChange"/>
</template>
<script setup>
  import { ref, nextTick, provide, reactive } from "vue"
  import { formatDate } from '@/utils/formatTime'
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import { ArrowDown, CircleClose } from '@element-plus/icons-vue'
  import { defaultProps, handleTree } from '@/utils/tree'
  import * as DeptApi from '@/api/system/dept'
  import MeetingForm from './meetingForm.vue'
  import { formatDateToYMD } from '@/utils/index'
  import personalDialog from './personalDialog.vue'
  const pageStatus = ref(1) // 1列表页  2新增编辑页
  const paramsData = reactive({
    code: '', // 会议编号
    name: '', // 会议名称
    leaderId: '', // 会议负责人
    leaderName: '', // 会议负责人名称
    department: '', // 会议部门
    type: '', // 会议类型
    meetingTime: '', // 会议时间
    pageNum: 1,
    pageSize: 10,
    ids: [],
  })
  const formRef = ref()
  const meetingTypeList = ref(getIntDictOptions(DICT_TYPE.MEETING_TYPE)) // 合同类型数据
  provide('meetingTypeList', meetingTypeList)
  const queryFormRef = ref() // 搜索表单
  const hoverIcon = ref(false) // 负责人图标是否悬停
  const personalDialogRef = ref(null)
  const loading = ref(false) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const tableData = ref([
    {
      id: 1, 
      code: '123', 
      name: '会议1', 
      type: '1', 
      leaderName: '张三', 
      departmentName: '部门1', 
      number: 10, 
      createTime: 1758187445000, 
      updateTime: 1748186445235, 
      creator: '张三',
      updater: '张三'
    },
    {
      id: 2, 
      code: '456', 
      name: '会议2', 
      type: '2', 
      leaderName: '李四', 
      departmentName: '部门2', 
      number: 10, 
      createTime: 1758187445000, 
      updateTime: 1748186445235, 
      creator: '李四',
      updater: '李四'
    },
  ]) // 列表的数据
  const tableColumn = ref([
    { prop: 'code', label: '会议编号', align: 'center', width: 160},
    { prop: 'name', label: '会议名称', align: 'center', },
    { prop: 'type', label: '会议类型', align: 'center'},
    { prop: 'leaderName', label: '会议负责人', align: 'center'},
    { prop: 'departmentName', label: '会议部门', align: 'center'},
    { prop: 'number', label: '会议人数', align: 'center'},
    { prop: 'createTime', label: '时间', align: 'center', width: 230},
    { prop: 'creator', label: '操作人', align: 'center', width: 220},
  ]) // 表格的列
  const addCompanies = () => {
    personalDialogRef.value.init()
  }
  const personalChange = (data) => {
    const { id, name } = data
    paramsData.leaderId = id
    paramsData.leaderName = name
  }
  const deptTree = ref() // 树形结构
  provide('deptTree', deptTree)
  /** 获得部门树 */
  const getTree = async () => {
    deptTree.value = []
    const data = await DeptApi.getSimpleDeptList()
    deptTree.value = handleTree(data)
  }
  const selectIds = ref([]) // 勾选的id
  const tableSelectionChange = (selection) => {
    selectIds.value = selection.map(item => item.id)
  }
  /** 搜索按钮操作 */
  const handleQuery = () => {
    paramsData.pageNo = 1
    getList()
  }
  /** 重置按钮操作 */
  const resetQuery = () => {
    paramsData.leaderName = ''
    queryFormRef.value.resetFields()
    handleQuery()
  }
  const goBack = () => {
    pageStatus.value = 1
    // getList()
  }
  const openForm = (id) => {
    pageStatus.value = 2
    nextTick(() => {
      formRef.value.init(id)
    })
  }
  provide('goBack', goBack)
  function findDeptNameById(tree, id) {
    for (const node of tree) {
      if (node.id === id) {
        return node.name
      }
      if (node.children && node.children.length > 0) {
        const name = findDeptNameById(node.children, id)
        if (name) return name
      }
    }
    return ''
  }
  provide('findDeptNameById', findDeptNameById)
  /** 初始化 **/
  onMounted( async() => {
    await getTree()
  })
</script>
<style lang="scss" scoped>
.coord{
  height: 32px;
  box-sizing: border-box;
  padding: 4px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  color: #636660;
  &:hover{
    border-color: #bcbdc0;
  }
}
.cursor_pointer{
  &:hover{
    border-color: rgb(64, 158, 255);
  }
}
.formInp{
  width: 100%;
}
</style>
